import { defineStore } from 'pinia'
import { ref } from 'vue'
import { projectApi } from '@/api/project'
import type { Project } from '@/types'

export const useProjectStore = defineStore('project', () => {
  // 状态
  const projects = ref<Project[]>([])
  const currentProject = ref<Project | null>(null)
  const loading = ref(false)

  // 获取所有项目
  const fetchProjects = async () => {
    loading.value = true
    try {
      const response = await projectApi.getAll()
      projects.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取我管理的项目
  const fetchMyProjects = async () => {
    loading.value = true
    try {
      const response = await projectApi.getMyProjects()
      projects.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 根据状态获取项目
  const fetchProjectsByStatus = async (status: string) => {
    loading.value = true
    try {
      const response = await projectApi.getByStatus(status)
      projects.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取项目详情
  const fetchProject = async (id: number) => {
    loading.value = true
    try {
      const response = await projectApi.getById(id)
      currentProject.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 创建项目
  const createProject = async (projectData: Omit<Project, 'id' | 'createdAt' | 'updatedAt'>) => {
    loading.value = true
    try {
      const response = await projectApi.create(projectData)
      projects.value.unshift(response)
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 更新项目
  const updateProject = async (id: number, projectData: Partial<Project>) => {
    loading.value = true
    try {
      const response = await projectApi.update(id, projectData)
      const index = projects.value.findIndex(p => p.id === id)
      if (index !== -1) {
        projects.value[index] = response
      }
      if (currentProject.value?.id === id) {
        currentProject.value = response
      }
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 删除项目
  const deleteProject = async (id: number) => {
    loading.value = true
    try {
      await projectApi.delete(id)
      projects.value = projects.value.filter(p => p.id !== id)
      if (currentProject.value?.id === id) {
        currentProject.value = null
      }
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 更新项目状态
  const updateProjectStatus = async (id: number, status: string) => {
    loading.value = true
    try {
      const response = await projectApi.updateStatus(id, status)
      const index = projects.value.findIndex(p => p.id === id)
      if (index !== -1) {
        projects.value[index] = response
      }
      if (currentProject.value?.id === id) {
        currentProject.value = response
      }
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 清空当前项目
  const clearCurrentProject = () => {
    currentProject.value = null
  }

  return {
    // 状态
    projects,
    currentProject,
    loading,
    // 方法
    fetchProjects,
    fetchMyProjects,
    fetchProjectsByStatus,
    fetchProject,
    createProject,
    updateProject,
    deleteProject,
    updateProjectStatus,
    clearCurrentProject
  }
})